<template>
  <el-form
    ref="formValidate"
    v-loading="loading"
    :model="formValidate"
    :rules="rules"
    label-width="100px"
    class="demo-formValidate"
  >
    <el-form-item :label="$t('shangPinTu')+'：'" prop="productId">
      <div class="upLoadPicBox" @click="changeGood">
        <div v-if="formValidate.productId" class="pictrue"><img :src="image"></div>
        <div v-else class="upLoad">
          <i class="el-icon-camera cameraIconfont" />
        </div>
      </div>
    </el-form-item>
    <el-form-item :label="$t('yongHuMingCheng')+'：'" prop="nickname">
      <el-input v-model="formValidate.nickname" type="text" />
    </el-form-item>
    <el-form-item :label="$t('pingJiaWenZi')+'：'" prop="comment">
      <el-input v-model="formValidate.comment" type="textarea" />
    </el-form-item>
    <el-form-item :label="$t('pingJiaXingJi')+'：'" prop="star" class="productScore">
      <el-rate v-model="formValidate.star" />
    </el-form-item>
    <el-form-item :label="$t('yongHuTouXiang')+'：'" prop="avatar">
      <div class="upLoadPicBox" @click="modalPicTap('1')">
        <div v-if="formValidate.avatar" class="pictrue"><img :src="formValidate.avatar"></div>
        <div v-else class="upLoad">
          <i class="el-icon-camera cameraIconfont" />
        </div>
      </div>
    </el-form-item>
    <el-form-item :label="$t('pingJiaTuPian')+'：'">
      <div class="acea-row">
        <div
          v-for="(item, index) in pics"
          :key="index"
          class="pictrue"
          draggable="false"
          @dragstart="handleDragStart($event, item)"
          @dragover.prevent="handleDragOver($event, item)"
          @dragenter="handleDragEnter($event, item)"
          @dragend="handleDragEnd($event, item)"
        >
          <img :src="item">
          <i class="el-icon-error btndel" @click="handleRemove(index)" />
          <!--<Button shape="circle" icon="md-close" class="btndel" @click.native="handleRemove(index)" />-->
        </div>
        <div v-if="pics < 10" class="upLoadPicBox" @click="modalPicTap('2')">
          <div class="upLoad">
            <i class="el-icon-camera cameraIconfont" />
          </div>
        </div>
      </div>
    </el-form-item>
    <el-form-item>
      <el-button size="mini" type="primary" :loading="loadingbtn" @click="submitForm('formValidate')">{{ $t('confirm') }}</el-button>
      <el-button size="mini" @click="resetForm('formValidate')">{{ $t('reset') }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { replyCreatApi, replyEditApi, replyInfoApi } from '@/api/store'
import { Debounce } from '@/utils/validate'
const defaultObj = {
  avatar: '',
  comment: '',
  nickname: '',
  pics: '',
  productId: '',
  star: null,
  attrValueId: 33
}
export default {
  name: 'CreatComment',
  props: {
    num: {
      type: Number,
      required: 0
    }
  },
  data() {
    var checkProductScore = (rule, value, callback) => {
      if (!value) {
        return callback(new Error(this.$t('pingJiaXingJiBuNengWeiKong')))
      } else {
        callback()
      }
    }
    return {
      loadingbtn: false,
      loading: false,
      pics: [],
      image: '',
      formValidate: Object.assign({}, defaultObj),
      rules: {
        avatar: [{ required: true, message: this.$t('qingXuanZeYongHuTouXiang'), trigger: 'change' }],
        productId: [{ required: true, message: this.$t('qingXuanZeShangPin'), trigger: 'change' }],
        comment: [{ required: true, message: this.$t('qingTianXiePingJiaNeiRong'), trigger: 'blur' }],
        nickname: [{ required: true, message: this.$t('qingTianXieYongHuMingCheng'), trigger: 'blur' }],
        pics: [{ required: true, message: this.$t('qingXuanZePingJiaTuPian'), trigger: 'change' }],
        star: [{ required: true, validator: checkProductScore, trigger: 'blur' }]
      }
    }
  },
  watch: {
    num: {
      handler: function(val) {
        this.resetForm('formValidate')
      },
      deep: true
    }
  },
  methods: {
    changeGood() {
      const _this = this
      this.$modalGoodList(function(row) {
        _this.image = row.image
        _this.formValidate.productId = row.id
      })
    },
    // 点击商品图
    modalPicTap(tit) {
      const _this = this
      _this.$modalUpload(
        function(img) {
          tit === '1'
            ? (_this.formValidate.avatar = img[0].sattDir)
            : img.map((item) => {
              _this.pics.push(item.sattDir)
            })
        },
        tit,
        'store'
      )
    },
    handleRemove(i) {
      this.pics.splice(i, 1)
    },
    submitForm: Debounce(function(formName) {
      this.formValidate.pics = this.pics.length > 0 ? JSON.stringify(this.pics) : ''
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loadingbtn = true
          replyCreatApi(this.formValidate)
            .then(() => {
              this.$message.success(this.$t('xinZengChengGong'))
              this.$emit('getList')
              this.loadingbtn = false
            })
            .catch(() => {
              this.loadingbtn = false
            })
        } else {
          return false
        }
      })
    }),
    resetForm(formName) {
      this.pics = []
      this.formValidate.avatar = ''
      this.$refs[formName].resetFields()
    },
    info() {
      this.loading = true
      replyInfoApi(this.formValidate)
        .then(res => {
          this.formValidate = res
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 移动
    handleDragStart(e, item) {
      this.dragging = item
    },
    handleDragEnd(e, item) {
      this.dragging = null
    },
    handleDragOver(e) {
      e.dataTransfer.dropEffect = 'move'
    },
    handleDragEnter(e, item) {
      e.dataTransfer.effectAllowed = 'move'
      if (item === this.dragging) {
        return
      }
      const newItems = [...this.pics]
      const src = newItems.indexOf(this.dragging)
      const dst = newItems.indexOf(item)
      newItems.splice(dst, 0, ...newItems.splice(src, 1))
      this.pics = newItems
    }
  }
}
</script>

<style scoped lang="scss">
.productScore {
  ::v-deep .el-rate {
    line-height: 2.4 !important;
  }
}
.pictrue {
  width: 60px;
  height: 60px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  margin-right: 10px;
  position: relative;
  cursor: pointer;
  img {
    width: 100%;
    height: 100%;
  }
}
.btndel {
  position: absolute;
  z-index: 1;
  width: 20px !important;
  height: 20px !important;
  left: 46px;
  top: -4px;
}
</style>
